package com.wuzhen.wzzt.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuzhen.wzzt.core.ret.RetCode;
import com.wuzhen.wzzt.core.ret.RetResponse;
import com.wuzhen.wzzt.core.ret.RetResult;
import com.wuzhen.wzzt.model.ActiveInfo;
import com.wuzhen.wzzt.service.ActiveService;
import com.wuzhen.wzzt.service.RedisService;
import com.wuzhen.wzzt.vo.ActiveTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: ActiveController
 * @author zhengzheng
 * @date 2022年4月1日 11:28
 */
@RestController
@RequestMapping("/fp")
@Api(tags = {"首页接口"}, description = "FristPageController", position=3)
public class FristPageController {
    private static Logger logger = LoggerFactory.getLogger(FristPageController.class);

    @Resource
    private ActiveService activeService;

    @Resource
    private RedisService redisService;


    @Value("${root.pic.url}")
    private String rootPicUrl;



    /**
     * 首页查询列表
     * @Description: 分页查询
     * @param activeTypeVo
     * @Reutrn RetResult<PageInfo<ActiveInfo>>
     */
    @PostMapping("/fpList")
    @ApiOperation(value = "首页列表", notes = "根据活动类型【1-智展，2-智会】查询首页活动列表")
    public RetResult<PageInfo<ActiveInfo>> fpList(@RequestBody ActiveTypeVo activeTypeVo) throws Exception {
        logger.info("首页列表查询，入参:activeTypeVo：{}",activeTypeVo);
        PageHelper.startPage(activeTypeVo.getPage(), activeTypeVo.getSize());
        List<ActiveInfo> list = activeService.selectFpList(activeTypeVo.getActiveType());
        list.forEach(activeInfo -> {
            this.setPicValue(activeInfo);
        });
        logger.info("首页列表查询返回记录条数：{}条",list.size());
        PageInfo<ActiveInfo> pageInfo = new PageInfo<ActiveInfo>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }

    private void setPicValue(ActiveInfo activeInfo){
        if(activeInfo!=null){
            if (activeInfo.getFpFilesName()!=null&&!"".equals(activeInfo.getFpFilesName())){
                activeInfo.setFpFilesName(rootPicUrl+"/fp/"+activeInfo.getFpFilesName());
            }
        }
    }

}